﻿<?php
/**
 * Book Manager Controller
 *
 * Sewiki is a experimental software for course
 * Software Engineering in HIT and it's devoloped
 * upon CodeIgniter Framework. More infomation of
 * the framework, please vist http://codeigniter.com/
 *
 * @package  CodeIgniter
 * @category controller/admin
 * @author   Oneplus <oneplus.lau@gmail.com>
 * @date     2011-06-13
 */
class BookManager extends CI_Controller {
	/**
	 * The constructor
	 *
	 * @access : public
	 * @return : void
	 */
	function __construct() {
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * Method Create Book
	 *
	 * Main controller method for admin create book
	 * infomation task logic
	 *
	 * @access : public
	 * @return : void
	 */
	public function createBook() {
		// step i Load create book form
		$this->load->view('admin/CreateBook');
		
		// step ii when submit button is pressed store book information in form to database
		if($this->input->post('submit')) {
			$this->load->model('book');
			
			$data = array(
				'title'       => $this->input->post('bname'),
				'description' => $this->input->post('bdesc'),
				'create_time' => date("Y-m-d H:i:s", time()),
				'uid'         => $this->session->userdata('uid'));
			
			$this->book->createBook($data);
			
			// step iii show success message
			$sessiondata = array('msg' => '创建图书成功');
			$this->session->set_userdata($sessiondata);
			
			redirect('browser/Browser/bookList');
		}
	}
	
	/**
	 * Method Modify Book
	 *
	 * Main controller method for admin modify book
	 * infomation task logic
	 *
	 * @access public
	 * @param  int, bid
	 * @return void
	 */
	public function modifyBook($bid = -1) {
		// step i fetch book information from database
		$query = $this->db->get_where('books', array('bid' => $bid));
		$book  = $query->row();
		
		$data = array(
			'title'       => $book->title,
			'description' => $book->description);
		// step ii load modify book form
		$this->load->view('admin/ModifyBook', $data);
		
		// step iii when modify button is pressed update book information in form to database
		if($this->input->post('modify')) {
			$this->load->model('book');
			
			$data = array(
				'title'       => $this->input->post('bname'),
				'description' => $this->input->post('bdesc'),
				'create_time' => time(),
				'uid'         => $this->session->userdata('uid'));
			
			$this->book->modifyBook($bid, $data);
			
			// step iv show success message
			$sessiondata = array('msg' => '修改图书成功');
			$this->session->set_userdata($sessiondata);

			redirect('browser/Browser/bookList');
		}
	}
}
?>